package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.SlidingLayout;
import cn.com.winning.ecare.utils.UserRecord;

/**
 * 自选超市主界面
 * 
 * @author Administrator
 */
public class Shop extends TabActivity
{
	private Shop oThis = this;
	TabHost tabHost;
	private TabWidget mTabWidget;
	public static Shop instance = null;
	private TextView toptitle;
	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
	private SlidingLayout slidingLayout;

	private LinearLayout content;
	Grideviewmenu gm;
	private GridView gridViewmain;

	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;
	private TextView nametv, tv;
	MyApplication myApp;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shop_main);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		myApp.getUserRecords().setUsername(myApp.getAccount());
		myApp.getUserRecords().setModule("Shop@自选超市");
		UserRecord.getInstance(myApp.getUserRecords()).init();
		instance = this;
		// 设置滑动菜单
		initSliding();
		toptitle = (TextView) this.findViewById(R.id.toptitle);
		tabHost = getTabHost();
		mTabWidget = (TabWidget) this.findViewById(android.R.id.tabs);
		Intent intent1 = new Intent();
		intent1.setClass(this, ShopMarket.class);
		Intent intent2 = new Intent();
		intent2.setClass(this, Shop3.class);
		Intent intent3 = new Intent();
		intent3.setClass(this, Shop4.class);

		// 新建一个tab并设置它的，Tag,标题，图标，内容
		tabHost.addTab(tabHost.newTabSpec("").setIndicator("").setContent(intent1));
		tabHost.addTab(tabHost.newTabSpec("").setIndicator("").setContent(intent2));
		tabHost.addTab(tabHost.newTabSpec("").setIndicator("").setContent(intent3));
		tabHost.setCurrentTab(0);// 设置初始选中状态为第一个tab
		iniTabHost();

	}

	private void initSliding()
	{
		menuButton = (ImageButton) findViewById(R.id.menuButton);

		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{

				oThis.onBackPressed();
			}
		});

	}

	private void iniTabHost()
	{
		// 初始化TabHost
		for (int i = 0; i < mTabWidget.getChildCount(); i++)
		{
			RelativeLayout view = (RelativeLayout) mTabWidget.getChildAt(i);

			TextView text = (TextView) view.findViewById(android.R.id.title);
			text.setTextSize(18);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text.getLayoutParams();
			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
			text.setLayoutParams(params);
			text.setGravity(Gravity.CENTER);
			view.setBackgroundColor(getResources().getColor(R.color.bottom_bgcolor));
			if (tabHost.getCurrentTab() == i)
			{
				if (i == 0)
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.market_selected));
				}
				else if (i == 1)
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.cart_selected));
				}
				else
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.order_selected));
				}
			}
			else
			{
				if (i == 0)
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.market_unselected));
				}
				else if (i == 1)
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.cart_unselected));
				}
				else
				{
					text.setBackgroundDrawable(getResources().getDrawable(R.drawable.order_unselected));
				}
			}
			int heightPx = DisplayUtil.dip2px(this, 60);
			mTabWidget.getChildAt(i).getLayoutParams().height = heightPx;
		}
		// TabHost切换
		tabHost.setOnTabChangedListener(new OnTabChangeListener()
		{

			public void onTabChanged(String tabId)
			{
//				if (Shop2.instance != null)
//				{// tabhost切换时关闭shop2，主要是购物后，点击购物车时要关闭
//					Shop2.instance.finish();
//				}
				// TODO Auto-generated method stub
				for (int i = 0; i < mTabWidget.getChildCount(); i++)
				{
					View view = mTabWidget.getChildAt(i);
					TextView text = (TextView) view.findViewById(android.R.id.title);
					if (tabHost.getCurrentTab() == i)
					{
						if (i == 0)
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.market_selected));
							toptitle.setText("自选超市");
						}
						else if (i == 1)
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.cart_selected));
							toptitle.setText("购物车");
						}
						else
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.order_selected));
							toptitle.setText("我的订单");
						}
					}
					else
					{
						if (i == 0)
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.market_unselected));
						}
						else if (i == 1)
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.cart_unselected));
						}
						else
						{
							text.setBackgroundDrawable(getResources().getDrawable(R.drawable.order_unselected));
						}
					}
				}
			}

		});
	}

}
